package com.healthcloud.service.patient.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.healthcloud.dao.patient.PatientDataDao;
import com.healthcloud.dao.patient.PatientDataSpecDao;
import com.healthcloud.entity.patient.PatientDataSpecEntity;
import com.healthcloud.service.patient.PatientDataSpecService;
import com.healthcloud.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service("patientDataSpecService")
public class PatienDataSpecServiceImpl implements PatientDataSpecService {
    @Autowired
    private PatientDataSpecDao patientDataSpecDao;
    @Autowired
    private PatientDataDao patientDataDao;

    @Override
    public void save(PatientDataSpecEntity patientDataSpec) {
        Date date = new Date();
        patientDataSpec.setCreateTime(date);
        patientDataSpec.setUpdateTime(date);
        patientDataSpec.setDeleted(0); // 未删除
        patientDataSpecDao.saveSpec(patientDataSpec);
        patientDataDao.updateSpecStatus(patientDataSpec.getIdCard(), 1);
    }

    @Override
    public List<PatientDataSpecEntity> listSpecByIdCard(PatientDataSpecEntity entity, PageParam pageParam) {
        return patientDataSpecDao.listSpecByIdCard(entity, pageParam);
    }

    @Override
    public Integer countSpecByIdCard(PatientDataSpecEntity entity) {
        return patientDataSpecDao.countSpecByIdCard(entity);
    }

    @Override
    public void update(PatientDataSpecEntity entity) {
        patientDataSpecDao.updateSpec(entity);
    }

    /**
     * 批量删除患者数据的特殊处理备注内容
     *
     * @param ids    pp_patient_data_spec 的 id 与, 拼接字符串
     * @param idCard 身份证号
     * @return true / false
     */
    @Override
    public Boolean batchDeleteSpecRemarks(String ids, String idCard) {
        // step1: 根据ids，批量删除患者数据的特殊处理备注内容
        List<Integer> idList = Lists.newArrayList();
        if (!ids.contains(",")) {
            idList.add(Integer.valueOf(ids));
        } else {
            String[] split = ids.split(",");
            for (String s : split) {
                idList.add(Integer.valueOf(s));
            }
        }

        int rows = patientDataSpecDao.batchDeleteSpecRemarks(idList);
        boolean specRemarkFlag = rows > 0;

        // step2: 若 pp_patient_data_spec 特殊处理内容全部被删除，则修改 pp_patient_data 的 spec_status 为 0
        boolean patientDataFlag = true;
        // 根据身份证号，查询有效的特殊处理备注条数
        int existRows = patientDataSpecDao.countNormalSpecRemark(idCard);
        if (existRows == 0) {
            // 若 pp_patient_data_spec 特殊处理内容全部被删除，则修改 pp_patient_data 的 spec_status 为 0
            int dataRows = patientDataDao.updateSpecStatus(idCard, 0);
            patientDataFlag = dataRows > 0;
        }
        return specRemarkFlag && patientDataFlag;
    }

}